package com.jk.controller;

import com.alibaba.fastjson.JSONObject;
import com.jk.cart.mapper.CartRepo;
import com.jk.cart.pojo.Cart;
import com.jk.flower.mapper.FlowerRepo;
import com.jk.flower.pojo.Flower;
import com.jk.order.mapper.OrderRepo;
import com.jk.order.pojo.CustomerOrder;
import com.jk.user.mapper.UserRepository;
import com.jk.user.pojo.User;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("cart")
public class CartController {


    @Autowired
    OrderRepo customerOrderRepo;

    @Autowired
    CartRepo cartRepo;

    @Autowired
    UserRepository userRepository;

    @Autowired
    FlowerRepo flowerRepo;

    //点击购买商品之后这些值会被赋值
    List<CustomerOrder> orderList=null; //公共的值 可以带过去的

    List<CustomerOrder> OrderinGoods = new ArrayList<>();
    /**页面**/
    @RequestMapping("tocart")
    public String toCart(Model model,HttpServletRequest request){
//        Flower flower = flowerRepo.findFlowerByFlowerId(orderList.get())
         List<CustomerOrder> orderList = customerOrderRepo.findCustomerOrdersByCustomerId((String)request.getSession().getAttribute("userid"));
         List<CustomerOrder> toList = new ArrayList<>();
         for( CustomerOrder order : orderList){
             if(order.getCustomerOrderState() == 0){
                 toList.add(order);
             }
         }
        OrderinGoods = toList;

        model.addAttribute("orderList",toList);
//        if(orderList != null){
//            model.addAttribute("orderList",orderList);
//        }else if(orderList == null){
//            model.addAttribute("orderList",orderList);
//        }
        return "/shoppingcart/cart";
    }

    String toUser = "";

    @RequestMapping("tocartconfim")
    public String toConfirm(Model model,HttpServletRequest request){
        model.addAttribute("allmoney",UUID.randomUUID().toString().replace("-","").toLowerCase());
        model.addAttribute("useraddress",toUser);
        return "/shoppingcart/affirmcart";
    }



    /**数据接口**/
    //点击购买之后写入数据库并且 跳转到这里 这里进值
    @ResponseBody
    @RequestMapping("chooseflo")
    public String getCart(Model model, HttpServletRequest request, HttpServletResponse response) throws ParseException {

        Double flowerprice = Double.parseDouble(request.getParameter("flowerprice"));
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        int quantity =Integer.parseInt(request.getParameter("quantity"));
        String flowerid = request.getParameter("flowerid");
        String userid = (String) request.getSession().getAttribute("userid");
        String prebuy = request.getParameter("cometime");
        prebuy = prebuy.replace("T"," ");
        Timestamp prebuytimeStamp = new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(prebuy).getTime());


        CustomerOrder customerOrder = new CustomerOrder();
        customerOrder.setCustomerOrderFinishDate(prebuytimeStamp);
        customerOrder.setId(UUID.randomUUID().toString().replace("-","").toLowerCase());
        customerOrder.setCustomerId(userid);
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        customerOrder.setCustomerOrderFinishDate(timestamp);
        String customerOrderId = UUID.randomUUID().toString().replace("-","").toLowerCase();
        customerOrder.setCustomerOrderId(customerOrderId);
        customerOrder.setCustomerOrderMoney(flowerprice*quantity);
        customerOrder.setCustomerOrderQuantity(quantity);
        customerOrder.setCustomerOrderState(0);
        customerOrder.setOrderStartDate(timestamp);
        customerOrder.setFlowerId(flowerid);
        customerOrder.setFlowerprice(flowerprice);

        customerOrderRepo.save(customerOrder);

        Cart cart = new Cart();
        String cartId = UUID.randomUUID().toString().replace("-","").toLowerCase();
        cart.setId(cartId);
        cart.setAllPrice(quantity*flowerprice);
        cart.setCustomerGoodsId(customerOrderId);
        cart.setUserId(userid);
        cartRepo.save(cart);

        User user = userRepository.findUserByUserName(userid);
        user.setCart(cartId);
//        model.addAttribute("customerOrder",customerOrder);
//        model.addAttribute("cart",cart);
//        model.addAttribute("user",user);

        orderList = customerOrderRepo.findCustomerOrdersByCustomerId(userid);
        ArrayList<CustomerOrder> toList = new ArrayList<>();
        for(int i = 0 ;i < orderList.size();i++){
            CustomerOrder customerOrder1 = orderList.get(i);
            if(customerOrder1.getCustomerOrderState() == 0){
                toList.add(customerOrder1);
            }
        }
        return null;
    }

    //设置值
    @RequestMapping("confirm")
    public String confirmCart(Model model, HttpServletRequest request, HttpServletResponse response,String allmoney,String userid){

        User user = userRepository.findUserByUserName(userid);

        toUser = user.getAddress();

        return "/shoppingcart/affirmcart";
    }

    @RequestMapping("jump")
    public String afterConfirm(HttpServletRequest request,Model model){

        String userid = (String) request.getSession().getAttribute("userid");
        List<CustomerOrder> customerOrdersList = customerOrderRepo.findCustomerOrdersByCustomerId(userid);
        for(CustomerOrder cO : customerOrdersList){
            if(cO.getCustomerOrderState() == 0){
                cO.setCustomerOrderState(1);
                customerOrderRepo.save(cO);
            }
        }
        return "/main/fk";
    }

    @RequestMapping("deleteorderingoods")
    @ResponseBody
    public String deleteorder(HttpServletRequest request,HttpServletResponse response,String cOid){

        JSONObject data =new JSONObject();
        customerOrderRepo.deleteByCustomerOrderId(cOid);

        data.put("status","20000");
        return data.toString();
    }






}
